import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_train.dart';
import 'package:flutter_learn_vi/src/bean/bean_video_course.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/page/train/train_teacher_course_item_widget.dart';
import 'package:flutter_learn_vi/src/page/train/train_teacher_course_video_item_widget.dart';
import 'package:flutter_learn_vi/src/utils/color_utils.dart';
import 'package:flutter_learn_vi/src/utils/string_utils.dart';

import 'model/train_model.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/14.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///

///学生
class TrainStudentSelectCoursePage extends StatefulWidget {
  //培训ID
  final TrainApproveBean approveBean;
  final bool isScaffold;

  const TrainStudentSelectCoursePage(
      {Key key, @required this.approveBean, this.isScaffold = true})
      : super(key: key);

  @override
  _TrainStudentSelectCoursePageState createState() =>
      _TrainStudentSelectCoursePageState();
}

class _TrainStudentSelectCoursePageState
    extends State<TrainStudentSelectCoursePage> with TrainModel {
  @override
  void initState() {
    super.initState();
    getMyTrainDetailsFunction();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.isScaffold) {
      return Scaffold(
        appBar: AppBar(
          title: Text("选择课程"),
        ),

        ///填充布局
        body: Container(
            width: double.infinity,
            height: double.infinity,
            child: buildBodyWidget()),
      );
    } else {
      return buildBodyWidget();
    }
  }

  Widget buildBodyWidget() {
    if (_isLoading) {
      return LoadingWidget();
    }
    if (_trainBean == null) {
      return NoDataWidget(
        clickCallBack: () {
          getMyTrainDetailsFunction();
        },
      );
    }
    return SingleChildScrollView(
      child: Container(
        width: double.infinity,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            buildTrainBase(),
            SizedBox(
              height: 0,
            ),
            buildCourseAndTeacher(),
          ],
        ),
      ),
    );
  }

  Widget buildTrainBase() {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.all(10),
      child: Card(
        child: Padding(
          padding: EdgeInsets.all(10),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: [
              Row(
                children: [
                  buildLeftTitle("培训名称"),
                  Expanded(
                    child: Text(
                      "${_trainBean.trainName}",
                      style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                  )
                ],
              ),
              SizedBox(
                height: 8,
              ),
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  buildLeftTitle("参与部门"),
                  Expanded(
                    child: Container(
                      margin: EdgeInsets.only(top: 2),
                      child: Wrap(
                        runSpacing: 10,
                        children: buildDepartment(),
                      ),
                    ),
                  )
                ],
              ),
              SizedBox(
                height: 8,
              ),
              (!widget.isScaffold && widget.approveBean.commitTime != null)
                  ? Row(
                      children: [
                        buildLeftTitle("申请时间"),
                        Expanded(
                          child: Text("${widget.approveBean.commitTime}"),
                        )
                      ],
                    )
                  : Container(),
              SizedBox(
                height: 8,
              ),
              widget.approveBean.approeTime != null
                  ? Row(
                      children: [
                        buildLeftTitle(widget.isScaffold ? "创建时间" : "审批时间"),
                        Expanded(
                          child: Text("${widget.approveBean.approeTime}"),
                        )
                      ],
                    )
                  : Container(),
            ],
          ),
        ),
      ),
    );
  }

  Widget buildLeftTitle(String title) {
    return Container(
      width: 68,
      child: Text("$title"),
    );
  }

  ///参与的部门
  List<Widget> buildDepartment() {
    String department = _trainBean.selectDepartment;
    if (StringUtils.isEmpty(department)) {
      return [buildDepartMentCell("全部部门")];
    } else {
      if (department.contains(",")) {
        List<String> list = department.split(",");
        List<Widget> widgetList = [];
        list.forEach((element) {
          widgetList.add(buildDepartMentCell(element));
        });
        return widgetList;
      } else {
        return [buildDepartMentCell("$department")];
      }
    }
  }

  ///教师与课程
  Widget buildCourseAndTeacher() {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.all(10),
      child: Card(
        child: Padding(
          padding: EdgeInsets.all(10),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: [
              Row(
                children: [
                  Container(
                    width: 4,
                    height: 14,
                    margin: EdgeInsets.only(right: 8),
                    color: Colors.blueAccent,
                  ),
                  Expanded(
                    child: Text(
                      "培训内容",
                      style: TextStyle(
                        fontSize: 14,
                      ),
                    ),
                  )
                ],
              ),
              SizedBox(
                height: 8,
              ),
              buildListView()
            ],
          ),
        ),
      ),
    );
  }

  ListView buildListView() {
    int count = 0;
    if (_trainBean.courseList != null) {
      count = _trainBean.courseList.length;
    }
    if (_trainBean.videoCourseEntityList != null) {
      count = count + _trainBean.videoCourseEntityList.length;
    }
    return ListView.builder(
      physics: NeverScrollableScrollPhysics(),
      shrinkWrap: true,
      itemCount: count,
      itemBuilder: (BuildContext context, int index) {
        if (_trainBean.courseList.length > 0 &&
            index < _trainBean.courseList.length) {
          CourseAndTeacherBean cubean = _trainBean.courseList[index];
          return TrainTeacherCourseItemWidget(
            cubean: cubean,
            index: index,
            isScaffold: widget.isScaffold,
          );
        } else {
          BeanVideoCourse beanVideoCourse = _trainBean
              .videoCourseEntityList[index - _trainBean.courseList.length];
          return TrainTeacherCourseVideoItemWidget(
            beanVideoCourse: beanVideoCourse,
            index: index,
            isScaffold: widget.isScaffold,
            trainId: _trainBean.trainId,
          );
        }
      },
    );
  }

  Widget buildDepartMentCell(String title) {
    return Stack(
      alignment: Alignment.center,
      children: [
        Container(
          margin: EdgeInsets.only(right: 10),
          padding: EdgeInsets.only(left: 6, right: 6, top: 1, bottom: 2),
          decoration: BoxDecoration(
              color: ColorUtils.getRandomColor(),
              borderRadius: BorderRadius.all(Radius.circular(20))),
          child: Text(
            "$title",
            style: TextStyle(color: Colors.white, fontSize: 9),
          ),
        )
      ],
    );
  }

  bool _isLoading = true;
  TrainBean _trainBean;

  Future<bool> getMyTrainDetailsFunction() async {
    _trainBean = await getTrainNetDatils2(trainId: widget.approveBean.trainId);
    _isLoading = false;
    if (mounted) {
      setState(() {});
    }
    return Future.value(true);
  }
}
